package com.group.zsxm.web.action;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.config.ParentPackage;
import org.apache.struts2.config.Result;
import org.apache.struts2.config.Results;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.group.zsxm.web.common.BaseAction;
@ParentPackage("appDefault")
@Scope("prototype")
@Controller
@Results( { @Result(name = "uploadok", value = "/WEB-INF/jsp/frame/fileUploadDone.jsp")
			})
public class UploadAction  extends BaseAction{
	private File[] uploads;

    private String[] uploadFileNames;

    private String[] uploadContentTypes;
    private String[] dir;

    private String[] targetFileName;
	public String[] getDir() {
		return dir;
	}

	public void setDir(String[] dir) {
		this.dir = dir;
	}

	public String[] getTargetFileName() {
		return targetFileName;
	}

	public void setTargetFileName(String[] targetFileName) {
		this.targetFileName = targetFileName;
	}

	 public File[] getUpload() {
	        return this.uploads;
	    }

	    public void setUpload(File[] upload) {
	        this.uploads = upload;
	    }

	    public String[] getUploadFileName() {
	        return this.uploadFileNames;
	    }

	    public void setUploadFileName(String[] uploadFileName) {
	        this.uploadFileNames = uploadFileName;
	    }

	    public String[] getUploadContentType() {
	        return this.uploadContentTypes;
	    }

	    public void setUploadContentType(String[] uploadContentType) {
	        this.uploadContentTypes = uploadContentType;
	    }


	//文件上传
	public String doUpload() throws Exception {
        // 获得upload路径的实际目�?
        String realPath = ServletActionContext.getRequest().getRealPath("/upload");
        //获得实际目录
        String targetDirectory = realPath;
        String[] mydir = new String[uploads.length];
        String[] tname = new String[uploads.length];
        for (int i = 0; i < uploads.length; i++) {
            // 生成保存文件的文件名�?
            tname[i] = generateFileName(uploadFileNames[i]);
            // 保存文件的路�?
            mydir[i] = targetDirectory + "\\" + tname[i];
            // 建立�?个目标文�?
            File target = new File(targetDirectory, tname[i]);
            // 将临时文件复制到目标文件
            FileUtils.copyFile(uploads[i], target);
        }
        setDir(mydir);
        setTargetFileName(tname);
        return "uploadok";
    }
}
